<template>
  <div class="home">
    <div class="zp">
      <img src="../../static/img/official.png" alt="" />
    </div>
    <van-swipe class="my-swipe" :autoplay="3000">
      <van-swipe-item v-for="item in swipe" :key="item.id">
        <img v-lazy="item.img_url" alt="" />
      </van-swipe-item>
    </van-swipe>
    <van-grid>
      <van-grid-item v-for="(item, index) in grids" :key="index" :to="item.go">
        <van-image :src="item.img"></van-image>
      </van-grid-item>
    </van-grid>
    <div class="goods-box">
      <div class="goods-title">
        {{ jps.title }}
      </div>
      <div class="goods-display">
        <div
          class="goods-item"
          v-for="item in jps.data"
          :key="item.id"
          @click="goDetails(item.id, jps.id)"
        >
          <div class="img">
            <img :src="item.img_url" alt="" />
          </div>
          <p class="good-name">{{ item.name }}</p>
          <p class="good-con">{{ item.content }}</p>
          <p class="good-price">￥{{ item.price }}</p>
        </div>
      </div>
    </div>
    <div class="goods-box">
      <div class="goods-title">
        {{ rms.title }}
      </div>
      <div class="goods-display">
        <div
          class="goods-item"
          v-for="item in rms.data"
          :key="item.id"
          @click="goDetails(item.id, rms.id)"
        >
          <div class="img">
            <img :src="item.img_url" alt="" />
          </div>
          <p class="good-name">{{ item.name }}</p>
          <p class="good-con">{{ item.content }}</p>
          <p class="good-price">￥{{ item.price }}</p>
        </div>
      </div>
    </div>
    <div class="goods-box">
      <div class="goods-title">
        {{ pjs.title }}
      </div>
      <div class="goods-display">
        <div
          class="goods-item"
          v-for="(item, index) in pjs.data"
          :key="index"
          @click="goDetails(item.id, pjs.id)"
        >
          <div class="img">
            <img :src="item.img_url" alt="" />
          </div>
          <p class="good-name">{{ item.name }}</p>
          <p class="good-con">{{ item.content }}</p>
          <p class="good-price">￥{{ item.price }}</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getlunbo, getHome } from '../../api'
export default {
  data() {
    return {
      swipe: [],
      grids: [{
        img: require("../../assets/img/xuangou.jpg"),
        go: "/home/phone"
      },
      {
        img: require("../../assets/img/peijian.jpg"),
        go: "/home/accessory"
      },
      {
        img: require("../../assets/img/pingbao.jpg"),
        go: "/home/protect"
      },
      {
        img: require("../../assets/img/dingzhi.jpg"),
        go: "/home/custom"
      }],
      jps: {},
      rms: {},
      pjs: {}
    }
  },
  methods: {
    async getLunbo() {
      const res = await getlunbo()
      this.swipe = res.data[0].data
    },
    async getHome() {
      const res = await getHome()
      res.data.forEach(item => {
        if (item.title === "精品专区") {
          this.jps = item
        }
        if (item.title === "热卖专区") {
          this.rms = item
        }
        if (item.title === "手机配件") {
          this.pjs = item
        }
      })
    },
    goDetails(id, dataid) {
      this.$router.push('/home/phoneDetails/' + dataid + '&' + id)
    }
  },
  created() {
    this.getLunbo()
    this.getHome()
  }
}
</script>
<style lang="scss" >
.zp img {
  width: 100%;
  height: 33px;
}
.my-swipe {
  height: 200px;
  .van-swipe-item {
    text-align: center;
    img {
      height: 200px;
    }
  }
}
ul {
  li {
    img {
      width: 100px;
      height: 100px;
    }
  }
}
.goods-box {
  margin-top: 0;
  text-align: center;
  .goods-title {
    height: 66px;
    line-height: 66px;
    background-color: #f7f7f7;
    font-weight: 400;
    font-size: 20px;
  }
  .goods-title::before,
  .goods-title::after {
    position: relative;
    margin: 0 3px;
    display: inline-block;
    content: " ";
    height: 0.5px;
    width: 20px;
    background-color: #9c9c9c;
    top: -5px;
  }
  .goods-display {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .goods-item {
      padding-bottom: 10px;
      width: 48%;
      .img {
        vertical-align: middle;
        width: 150px;
        height: 150px;
        img {
          margin-top: 20px;
          margin-left: 43px;
          width: 80%;
          height: 80%;
        }
      }
      .good-name {
        height: 26px;
        width: 80%;
        margin: 0 auto;
        font-weight: 700;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .good-con {
        height: 26px;
        width: 80%;
        margin: 0 auto;
        font-size: 12px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .good-price {
        width: 80%;
        margin: 0 15px;
        color: red;
      }
    }
  }
}
</style>